/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.workforce.common.tags;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import org.workforce.util.Utils;


/**
 *
 * @author neo
 */
public class FormTag extends TagSupport {

    protected String action;
    protected String enctype;
    protected String sid;
    protected String method;

    @Override
    public int doStartTag() throws JspException {
        JspWriter out = this.pageContext.getOut();
        StringBuffer s = new StringBuffer();
        s.append("<form method=\"");
        if( method != null ) {
            s.append(method);
        } else {
            s.append("GET");
        }
        s.append("\" ");
        s.append("action=\"").append(action).append("\" ");
        String fname = "formId_" + Utils.generateId();
        if( id != null ) {
        s.append("id=\"").append(id).append("\" ");
        }
        s.append("name=\"").append(fname).append("\"");
        if( enctype != null ) {
            if( enctype.equals("true") ) {
                s.append(" enctype=\"multipart/form-data\"");
            }
        }
        s.append(">\n");
        try {
            out.print(s.toString());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return EVAL_BODY_INCLUDE;
    }

    @Override
    public int doEndTag() throws JspException {
        JspWriter out = this.pageContext.getOut();
        StringBuffer s = new StringBuffer();
        //s.append("<div id=\"calendar\" style=\"position: absolute\"></div>");
        s.append("</form>\n");
        try {
            out.print(s.toString());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return super.doEndTag();
    }

    public void setAction(String action) {
        this.action = action;
    }

    public void setEnctype(String enctype) {
        this.enctype = enctype;
    }

    public void setId(String sid) {
        this.sid = sid;
    }
    
}
